package com.bobi.tfca.ui.mainnavigation.message.chatroom

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GroupDetailFragmentVM : ViewModel() {

    /**
     * 群聊界面开始的头像
     */
    val headMembers by lazy {
        MutableLiveData(ArrayList<MemberResponse>())
    }

    /**
     * 解散群聊
     */
    fun disbandGroup(
        groupId: Int,
        publicViewModel: PublicViewModel,
        groupDetailFragment: GroupDetailFragment
    ) {
        publicViewModel.apply {
            request(MessageAPI::class.java).disbandGroup(groupId).getResponse {
                it.collect{
                    when(it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val code = it.data?.code
                                if (code == 200) {
                                    // publicViewModel清空chatRoom
                                    publicViewModel.resetChatRoom()
                                    // 返回到消息fragment
                                    groupDetailFragment.requireActivity().findNavController(R.id.app_nav).popBackStack(R.id.mainNavigationFragment, false)
                                    Toast.makeText(groupDetailFragment.requireActivity(), "解散群聊成功", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(groupDetailFragment.requireActivity(), "解散群聊失败", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun getGroupMemberHeadList(
        publicViewModel: PublicViewModel,
        groupDetailFragment: GroupDetailFragment
    ) {
        val groupId = publicViewModel.chatRoom.value!!.groupId
        publicViewModel.apply {
            request(MessageAPI::class.java).getGroupMemberHeadList(groupId).getResponse {
                it.collect{
                    when(it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    this@GroupDetailFragmentVM.headMembers.value = result
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}